package cn.topcode.unicorn.utils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Unicorn on 2017/9/26.
 */
public class InValidator implements ConstraintValidator<In, String> {

    private String[] values;

    private boolean required = true;

    @Override
    public void initialize(In in) {
        values = in.values();
        if(values == null) values = new String[]{};
        required = in.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(value == null) {
            return !required;
        }
        for(String v : values) {
            if(value.equals(v)) {
                return true;
            }
        }
        return false;
    }
}
